﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
namespace Congducsan
{
    public partial class FrmThanhop : Form
    {
        public FrmThanhop()
        {
            InitializeComponent();
        }

        private void FrmThanhop_FormClosing(object sender, FormClosingEventArgs e)
        {
            DradApplication.DradApp.FormMain.Show();
        }

        private void FrmThanhop_Resize(object sender, EventArgs e)
        {
            this.Size = new Size(616, 533);
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            PrThanhop frmHop = new PrThanhop();
            frmHop.Show();
            this.Hide();
        }

        private void FrmThanhop_Load(object sender, EventArgs e)
        {
            loadSettingReg();
        }

            //Lưu settings vào registry
        private void saveSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            string Values = "";
            string[] Var1 = regKey.GetValue("KtThan").ToString().Split('\t');
            Values = txtDaymongMin.Text;//0
            Values = Values +"\t"+ Var1[1];//1
            Values = Values +"\t"+ txtDaydemmong.Text;//2
            Values = Values +"\t"+ txtCachkhoangday.Text;//3
            Values = Values +"\t"+ txtSoday.Text;//4
            Values = Values +"\t"+ txtLoaicong.Text;//5
            Values = Values +"\t"+ Var1[6];//6
            Values = Values +"\t"+ txtDayong.Text;//7
            Values = Values +"\t"+ txtKhaudien.Text;//8
            Values = Values +"\t"+ txtCaocong.Text;//9
            Values = Values +"\t"+ txtDayban.Text;//10
            Values = Values +"\t"+ txtCotai.Text;//11
            Values = Values +"\t"+ Var1[12];//12
            Values = Values +"\t"+ txtMomong.Text;//13
            Values = Values +"\t"+ Var1[14];//14
            Values = Values +"\t"+ Var1[15];//15

            string Values1;
            string[] Var2 = regKey.GetValue("Vatlieu").ToString().Split('\t');
            Values1 = Var2[0];
            Values1 = Values1 + "\t" + Var2[1];
            Values1 = Values1 + "\t" + Var2[2];
            Values1 = Values1 + "\t" + Var2[3];
            Values1 = Values1 + "\t" + Var2[4];
            Values1 = Values1 + "\t" + Var2[5];
            Values1 = Values1 + "\t" + Var2[6];
            Values1 = Values1 + "\t" + Var2[7];
            Values1 = Values1 + "\t" + Var2[8];
            Values1 = Values1 + "\t" + txtVlMong.Text;
            Values1 = Values1 + "\t" + Var2[10];

            regKey.SetValue("KtThan", Values);
            regKey.SetValue("Vatlieu", Values1);
            regKey.SetValue("GlobalLytrinh", txtLytrinh.Text);
            regKey.Close();
        }
           //Load settings từ registry vào form
        private void loadSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            RegistryKey testKey = regKey.OpenSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            if (testKey != null)
            {
                regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
                string str = regKey.GetValue("KtThan").ToString();
                string[] STR = str.Split('\t');
                int i = 0;
                txtDaymongMin.Text=STR[i++];//0
                //Var(1).Text;//1
                i++;
                txtDaydemmong.Text = STR[i++];//2
                txtCachkhoangday.Text = STR[i++];//3
                txtSoday.Text = STR[i++];//4
                txtLoaicong.Text = STR[i++];//5
                //Var(6).Text;//6
                i++;
                txtDayong.Text = STR[i++];//7
                txtKhaudien.Text = STR[i++];//8
                txtCaocong.Text = STR[i++];//9
                txtDayban.Text = STR[i++];//10
                txtCotai.Text = STR[i++];//11
                //Var(12).Text;//12
                i++;
                txtMomong.Text = STR[i++];//13
                //Var(14).Text;//14
                //Var(15).Text;//15
                string[] Var2 = regKey.GetValue("Vatlieu").ToString().Split('\t');
                txtVlMong.Text = Var2[9];
                regKey.Close();
            }
        }
            // Save setting vào file
        private void saveSettingFile()
        {
            SaveFileDialog svDialog = new SaveFileDialog();
            svDialog.Filter = "Data files (*.hop)|*.hop";
            svDialog.InitialDirectory = "C://";
            svDialog.RestoreDirectory = true;
            string filename = "";
            if (svDialog.ShowDialog() == DialogResult.OK)
            {
                filename = svDialog.FileName;
                string Str;
                //Than cong
                Str = "------THAN CONG------"+"\r\n";
                Str = Str+ "So day cong:" +"\t"+ txtSoday .Text+"\r\n";
                Str = Str+ "Khoang cach day:" +"\t"+ txtCachkhoangday .Text+"\r\n";
                //Mong cong
                Str = Str+ "------MONG CONG------" +"\r\n";
                Str = Str+ "Day mong H1:" +"\t"+ txtDaymongMin .Text+"\r\n";
                Str = Str+ "Day dem lot H2:" +"\t"+ txtDaydemmong .Text+"\r\n";
                Str = Str+ "Mo rong mong m1:" +"\t"+ txtMomong .Text+"\r\n";
                Str = Str+ "Mo rong mong m2:" +"\t"+ txtLopdemm .Text+"\r\n";
                Str = Str + "Vat lieu mong:" + "\t" + txtVlMong.Text;
                //Ghi file dữ liệu
                FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(Str);
                sw.Close();
                fs.Close();
                MessageBox.Show("OK", "Thông báo");
            }
        }

             // load setting từ file vào form
        private void loadSettingFile()
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Data files (*.hop)|*.hop";
            openDialog.InitialDirectory = @"C:\";
            openDialog.FilterIndex = 2;
            openDialog.RestoreDirectory = true;
            openDialog.Multiselect = false;
            string filename;
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                filename = openDialog.FileName;
                FileStream fs = new FileStream(filename, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string str = sr.ReadToEnd();
                fs.Close();
                fs.Close();
                string[] Values = str.Split('\n');
                int i = 1;
                //Than cong
                txtSoday.Text=Values[i].Split('\t')[1]; i++;
                txtCachkhoangday.Text=Values[i].Split('\t')[1]; i++;
                //Mong cong
                i = i + 1;
                txtDaymongMin.Text=Values[i].Split('\t')[1]; i++;
                int j = i; i = i + 1;
                txtMomong.Text=Values[i].Split('\t')[1]; i++;
                txtLopdemm.Text=Values[i].Split('\t')[1]; i++;
                txtVlMong.Text=Values[i].Split('\t')[1]; i++;
                //Thoat khoi su kien click_txtVlMong()
                txtDaydemmong.Text = Values[j].Split('\t')[1]; i++;
                MessageBox.Show("OK", "Thông báo");
            }
        }

        private void SaveData_Click(object sender, EventArgs e)
        {
            saveSettingFile();
        }

        private void LoadData_Click(object sender, EventArgs e)
        {
            loadSettingFile();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmThanhop_FormClosed(object sender, FormClosedEventArgs e)
        {
            saveSettingReg();
        }
    }
}
